<html>
<head>
	<script type="text/javascript">
		var aJaxURL	= "server-side/view/barcode.action.php";		//server side folder url
		var seoyURL	= "server-side/seoy/seoy.action.php";			//server side folder url
		var tName	= "example";									//table name
		var fName	= "add-edit-form";
										//form name

		$(document).ready(function () {
			LoadTable();

			/* Add Button ID, Delete Button ID */
			GetButtons("add_button", "delete_button");
			GetButtons("print_barcode_button", "");
			SetEvents("add_button", "delete_button", "check-all", tName, fName, aJaxURL);
		});

        //SeoYyy
		$(document.body).click(function (e) {
        	$("#barcode_client").autocomplete("close");
        	$("#barcode_cartridge").autocomplete("close");
        });

		function LoadTable(){
			/* Table ID, aJaxURL, Action, Colum Number, Custom Request, Hidden Colum, Menu Array */
			GetDataTable(tName, aJaxURL, "get_list", 7, "", 0, "");
		}

		function LoadDialog(){
			SeoY("barcode_client", seoyURL, "barcode_client", "", 0);
			SeoY("barcode_cartridge", seoyURL, "barcode_cartridge", "", 0);

			var id = $("#barcode_id").val();
			if(empty(id)){
				var buttons = {
		            "print": {
		                text: "შენახვა + დაბეჭდვა",
		                id: "print-save-dialog",
		                click: function () {
		                }
		            },
		            "cancel": {
		                text: "დახურვა",
		                id: "cancel-dialog",
		                click: function () {
		                	$(this).dialog("close");
		                }
					}
				};
			}else{
				var buttons = {
		            "print": {
		                text: "დაბეჭდვა",
		                id: "print-dialog",
		                click: function () {
		                }
		            },
		            "print-save": {
		                text: "შენახვა",
		                id: "save-dialog",
		                click: function () {
		                }
		            },
		            "cancel": {
		                text: "დახურვა",
		                id: "cancel-dialog",
		                click: function () {
		                	$(this).dialog("close");
		                }
					}
				};
				$(".dialog-form-table input, .dialog-form-table select, .dialog-form-table button").prop('disabled', true);
			}


			/* Dialog Form Selector Name, Buttons Array */
			GetDialog(fName, 500, "auto", buttons);
		}

	    // Add - Save
		$(document).on("click", "#save-dialog", function () {
			SaveBarcode(false);
		});

        $(document).on("click", "#print-dialog", function (event) {
        	var id = $("#barcode_id").val();

        	GetPrintPage("[" + id + "]");
		});

        $(document).on("click", "#print-save-dialog", function (event) {
        	SaveBarcode(true);
		});

		$(document).on("change", "#type", function (event) {
			$("#barcode_client").focus();
		});

		$(document).on("click", "#barcode_client-widget", function (event) {
			$("#barcode_cartridge").focus();
		});

		$(document).on("click", "#barcode_cartridge-widget", function (event) {
			$("#old_barcode").focus();
		});

        function SaveBarcode(print){
			param = new Object();
            //Action
	    	param.act	= "save_barcode";

		    param.id	= $("#barcode_id").val();

		    param.t		= $("#type").val();
		    param.bcl	= $("#barcode_client").val();
		    param.bct	= $("#barcode_cartridge").val();
		    param.ob    = $("#old_barcode").val();
		    param.c     = $("#count").val();
		    param.com	= $("#comment").val();

		    if(param.t == 0){
				alert("შეავსეთ კარტრიჯის ტიპი!");
			}else if(param.bcl == 0){
				alert("შეავსეთ კლიენტის დასახელება!");
			}else if(param.bct == 0){
				alert("შეავსეთ კარტრიჯის დასახელება!");
			}else{
			 $.ajax({
			        url: aJaxURL,
				    data: param,
			        success: function(data) {
						if(typeof(data.error) != "undefined"){
							if(data.error != ""){
								alert(data.error);
							}else{
								LoadTable();
				        		CloseDialog(fName);
								if(print){
									GetPrintPage(data.barcode_list);
								}
							}
						}
				    }
			    });
				}
			}


        function GetPrintPage(barcode_list){
	    	param = new Object();

            //Action
	    	param.act	= "get_barcode";

		    param.list	= barcode_list;

	    	var data = GetAjaxData(param);
	    	var link = GetRootDIR();

	    	link = link + "server-side/print/print.action.php?" + data;

	    	var newWin = window.open(link, "JSSite", "width=800,height=800,resizable=yes,scrollbars=yes,status=yes");
	    	newWin.focus();
	    	newWin.onload = function() {
	    		newWin.print();
	    		setInterval(function(){newWin.close()},3000);
		    }
        }

        $(document).on("click", ".combobox", function (event) {
	    	var i = $(this).text();
			$("#" + i).autocomplete( "search", "" );
		});


        $(document).on("click", "#print_barcode_button", function (event) {
			param = new Object();

            //Action
	    	param.act	= "print_barcode";
		    $.ajax({
		        url: aJaxURL,
			    data: param,
		        success: function(data) {
					if(typeof(data.error) != "undefined"){
						if(data.error != ""){
							alert(data.error);
						}else{
							var buttons = {
						            "cancel": {
						                text: "დახურვა",
						                id: "cancel-dialog",
						                click: function () {
						                	$(this).dialog("close");
						                }
						            }
								};
							$("#print-dialog-form").html(data.printpage);
							GetDialog("print-dialog-form", 500, "auto", buttons);
							$("#print_barodeee").focus();
						}
					}
			    }
		    });

		});
        $(document).on("keydown", "#print_barodeee", function (event) {
			if (event.keyCode == $.ui.keyCode.ENTER){

		    	var act	    = "get_print_barcode";
		    	var list	= $("#print_barodeee").val()


		    	var link = GetRootDIR();

		    	link = link + "server-side/print/print.action.php?act="+act+"&list="+list;

		    	var newWin = window.open(link, "JSSite", "width=800,height=800,resizable=yes,scrollbars=yes,status=yes");
		    	newWin.focus();
		    	newWin.onload = function() {
		    		newWin.print();
		    		setInterval(function(){newWin.close()},3000);
			    }
		    	$("#print_barodeee").val('');
		    	$("#print_barodeee").focus();
				event.preventDefault();					//Clear Dialog Default <b>keypress<b> Event
            }
		});
    </script>
</head>

<body>
    <div id="dt_example" class="ex_highlight_row">
        <div id="container">
            <div id="dynamic">
                <h2 align="center">კარტრიჯის შტრიხ კოდები</h2>
	        	<div id="button_area">
	        		<button id="add_button">შტრიხ დამატება</button><button id="delete_button">წაშლა</button><button id="print_barcode_button">შტრიხკოდის ბეჭდვა</button>
	        	</div>
                <table class="display" id="example">
                    <thead>
                        <tr id="datatable_header">
                            <th>ID</th>
                            <th style="width: 100%">კლიენტი</th>
                            <th style="width: 100%">მოდელი</th>
                            <th class="min">ძველი შტრიხ კოდი</th>
                            <th class="min">შტრიხ კოდი</th>
                            <th class="min">შექმნის თარიღი</th>
                            <th class="min">შენიშვნა</th>
                            <th class="check">#</th>
                        </tr>
                    </thead>
                    <thead>
                        <tr class="search_header">
                            <th class="colum_hidden">
                            	<input type="text" name="search_id" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_client" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_model" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_oldbarcode" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_position" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_address" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_name" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                            	<input type="checkbox" name="check-all" id="check-all">
                            </th>
                        </tr>
                    </thead>
                </table>
            </div>
            <div class="spacer">
            </div>
        </div>
    </div>
    <div id="print-dialog-form" class="form-dialog" title="შტრიხ კოდები">
    	<!-- aJax -->
	</div>
    <!-- jQuery Dialog -->
    <div id="add-edit-form" class="form-dialog" title="შტრიხ კოდები">
    	<!-- aJax -->
	</div>
</body>
</html>